﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyTruongHoc_App.MyDataSet;
using QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters;
using System.Configuration;

namespace QuanLyTruongHoc_App
{
    public partial class XetLenLop : Form
    {
        // Column name 
        public const String CLN_ID = "id";
        public const String CLN_ID_LOP = "idLop";
        public const String CLN_HO_DEM = "hodem";
        public const String CLN_TEN = "ten";
        public const String CLN_NGAY_SINH = "NgaySinh";
        public const String CLN_GIOI_TINH = "GioiTinh";
        public const String CLN_HK_I = "hocky1";
        public const String CLN_HK_II = "hocky2";
        public const String CLN_CA_NAM = "CaNam";
        public const String CLN_HOC_LUC = "HocLuc";
        public const String CLN_HANH_KIEM = "HanhKiem";
        public const String CLN_LEN_LOP = "LenLop";
        public const String CLN_LOP_KE = "LopKeTiep";

        // -----------
        protected DataTable data, dataLop;
        protected int state,
                    idNamHoc;

        protected BindingSource tblBinding;
        protected DataGridViewTextBoxColumn grc_id;
        protected DataGridViewComboBoxColumn grc_Lop;

        public XetLenLop()
        {
            InitializeComponent();
            grc_id = new DataGridViewTextBoxColumn();
            grc_Lop = new DataGridViewComboBoxColumn();
            grc_NgaySinh.DefaultCellStyle.Format = "dd/MM/yyy";
            dg_DanhSach.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dg_DanhSach_DataBindingComplete);
        }

        private void checkUserPower()
        {
            int pow = PublicLib.getUserPower(PConst.QUYEN_XET_LEN_LOP);
            // user co quyen view
            tscb_NamHoc.Enabled = ((pow & PConst.POW_VIEW) > 0);
            // user co quyen them
            //tsb_TaoMoi.Enabled = ((pow & PConst.POW_ADD) > 0);
            // user co quyen edit
            tsb_Luu.Enabled = ((pow & PConst.POW_EDIT) > 0);
            // user co delete
            //tsb_Delete.Enabled = ((pow & PConst.POW_DEL) > 0);
        }

        private void QuanLyGiaoVien_Load(object sender, EventArgs e)
        {
            refreshData();
        }

        private void reloadComboBox()
        {
            state = PConst.STATE_LOAD;
            // Lop
            MyDataSet.QLTHTableAdapters.tbl_LopHocTableAdapter lopAdap = new tbl_LopHocTableAdapter();
            lopAdap.Connection.ConnectionString = PublicLib.getStrConn();
            lopAdap.Fill(qLTH.tbl_LopHoc);

            tscb_Lop.Items.Clear();
            for (int i = 0; i < qLTH.tbl_LopHoc.Rows.Count; i++)
            {
                tscb_Lop.Items.Add(qLTH.tbl_LopHoc.Rows[i][qLTH.tbl_LopHoc.TenLopColumn].ToString());
            }
            tscb_Lop.SelectedIndex = 0;
            tscb_Lop.Items.Add(MSG.MNU_ALL);
            // --------------
            // Nam Hoc
            MyDataSet.QLTHTableAdapters.tbl_NamHocTableAdapter namAdap = new tbl_NamHocTableAdapter();
            namAdap.Connection.ConnectionString = PublicLib.getStrConn();
            namAdap.FillByFlag(qLTH.tbl_NamHoc, PConst.FLAG_NONE);

            tscb_NamHoc.Items.Clear();
            for (int i = 0; i < qLTH.tbl_NamHoc.Rows.Count; i++)
            {
                tscb_NamHoc.Items.Add(qLTH.tbl_NamHoc.Rows[i][qLTH.tbl_NamHoc.TenColumn].ToString());
            }

            if (tscb_NamHoc.Items.Count > 0)
                tscb_NamHoc.SelectedIndex = tscb_NamHoc.Items.Count - 1;
            // --------------
            state = PConst.STATE_LOADED;
        }

        private void reloadDataTable()
        {
            if (tscb_Lop.SelectedIndex == -1) return;
            int idLop = (int)qLTH.tbl_LopHoc.Rows[tscb_Lop.SelectedIndex][qLTH.tbl_LopHoc.idColumn];
            data = new DataTable();

            tbl_HocLucTableAdapter hocLucAdap = new tbl_HocLucTableAdapter();
            hocLucAdap.Connection.ConnectionString = PublicLib.getStrConn();
            hocLucAdap.FillByFlag(qLTH.tbl_HocLuc, PConst.FLAG_NONE);

            tbl_HanhKiemTableAdapter hanhkiemAdap = new tbl_HanhKiemTableAdapter();
            hanhkiemAdap.Connection.ConnectionString = PublicLib.getStrConn();
            hanhkiemAdap.FillByFlag(qLTH.tbl_HanhKiem, PConst.FLAG_NONE);

            tbl_LenLopTableAdapter llAdap = new tbl_LenLopTableAdapter();
            llAdap.Connection.ConnectionString = PublicLib.getStrConn();
            llAdap.FillByFlag(qLTH.tbl_LenLop, PConst.FLAG_NONE);

            // Add Ten Hoc Sinh column
            data.Columns.Add(CLN_ID, Type.GetType("System.Int32")); // id
            data.Columns.Add(CLN_ID_LOP, Type.GetType("System.Int32")); // lop hien tai
            data.Columns.Add(CLN_HO_DEM, Type.GetType("System.String")); // Ho Dem
            data.Columns.Add(CLN_TEN, Type.GetType("System.String")); // Ten
            data.Columns.Add(CLN_GIOI_TINH, Type.GetType("System.String")); // Gioi Tinh
            data.Columns.Add(CLN_NGAY_SINH, Type.GetType("System.DateTime")); // Ngay Sinh
            
            // ---------
            // Diem TB cac mon
            data.Columns.Add(CLN_HK_I, Type.GetType("System.Single"));
            data.Columns.Add(CLN_HK_II, Type.GetType("System.Single"));
            data.Columns.Add(CLN_CA_NAM, Type.GetType("System.Single"));
            // Hoc Luc, Hanh Kiem, Duoc Len lop
            data.Columns.Add(CLN_HOC_LUC, Type.GetType("System.Int32"));
            data.Columns.Add(CLN_HANH_KIEM, Type.GetType("System.Int32"));
            data.Columns.Add(CLN_LEN_LOP, Type.GetType("System.Int32"));

            data.Columns.Add(CLN_LOP_KE, Type.GetType("System.Int32"));
            // ---------
            tblBinding = new BindingSource();
            dg_DanhSach.Columns.Clear();
            dg_DanhSach.DataSource = tblBinding;
            // ------------------------
            #region setting column
            dataLop = new DataTable();
            dataLop.Columns.Add("id", Type.GetType("System.Int32"));
            dataLop.Columns.Add("ten", Type.GetType("System.String"));

            for (int i = 0; i < qLTH.tbl_LopHoc.Rows.Count; i++)
            {
                dataLop.Rows.Add(qLTH.tbl_LopHoc[i][qLTH.tbl_LopHoc.idColumn],
                                 qLTH.tbl_LopHoc[i][qLTH.tbl_LopHoc.TenLopColumn]);
            }
            dataLop.Rows.Add(-1, MSG.MNU_TOT_NGHIEP);

            this.dg_DanhSach.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.grc_STT,
            grc_id,
            grc_Lop,
            this.grc_HoDem,
            this.grc_Ten,
            this.grc_GioiTinh,
            this.grc_NgaySinh,
            this.grc_HK1,
            this.grc_HK2,
            this.grc_CaNam,
            this.grc_HocLuc1,
            this.grc_HanhKiem1,
            this.grc_LenLop1,
            this.grc_LopKe});
            // 
            // grc_STT
            // 
            this.grc_STT.HeaderText = "STT";
            this.grc_STT.Name = "grc_STT";
            this.grc_STT.Width = 40;
            // 
            // grc_id
            // 
            this.grc_id.Visible = false;
            grc_id.Name = "grc_id";
            //
            // grc_lop
            //
            grc_Lop.HeaderText = "Lớp";
            grc_Lop.Name = "grc_Lop";
            grc_Lop.Width = 80;
            grc_Lop.DataSource = dataLop;
            grc_Lop.DisplayMember = "ten";
            grc_Lop.ValueMember = "id";

            // 
            // grc_HoDem
            // 
            this.grc_HoDem.HeaderText = "Họ Đệm";
            this.grc_HoDem.Name = "grc_HoDem";
            // 
            // grc_Ten
            // 
            this.grc_Ten.HeaderText = "Tên";
            this.grc_Ten.Name = "grc_Ten";
            this.grc_Ten.Width = 60;
            // 
            // grc_GioiTinh
            // 
            this.grc_GioiTinh.HeaderText = "Giới Tính";
            this.grc_GioiTinh.Name = "grc_GioiTinh";
            this.grc_GioiTinh.Width = 80;
            // 
            // grc_NgaySinh
            // 
            this.grc_NgaySinh.HeaderText = "Ngày Sinh";
            this.grc_NgaySinh.Name = "grc_NgaySinh";
            this.grc_NgaySinh.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            this.grc_NgaySinh.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
            this.grc_NgaySinh.Width = 80;
            // 
            // grc_HK1
            // 
            this.grc_HK1.HeaderText = "TB HK I";
            this.grc_HK1.Name = "grc_HK1";
            this.grc_HK1.Width = 50;
            // 
            // grc_HK2
            // 
            this.grc_HK2.HeaderText = "TB HK II";
            this.grc_HK2.Name = "grc_HK2";
            this.grc_HK2.Width = 50;
            // 
            // grc_CaNam
            // 
            this.grc_CaNam.HeaderText = "TB CN";
            this.grc_CaNam.Name = "grc_CaNam";
            this.grc_CaNam.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            this.grc_CaNam.Width = 50;
            // 
            // grc_HocLuc1
            // 
            this.grc_HocLuc1.HeaderText = "Học Lực";
            this.grc_HocLuc1.Name = "grc_HocLuc1";
            this.grc_HocLuc1.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            this.grc_HocLuc1.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            this.grc_HocLuc1.Width = 80;
            grc_HocLuc1.DataSource = qLTH.tbl_HocLuc;
            grc_HocLuc1.DisplayMember = qLTH.tbl_HocLuc.TenColumn.ColumnName;
            grc_HocLuc1.ValueMember = qLTH.tbl_HocLuc.idColumn.ColumnName;

            // 
            // grc_HanhKiem1
            // 
            this.grc_HanhKiem1.HeaderText = "Hạnh Kiểm";
            this.grc_HanhKiem1.Name = "grc_HanhKiem1";
            this.grc_HanhKiem1.Width = 80;
            grc_HanhKiem1.DataSource = qLTH.tbl_HanhKiem;
            grc_HanhKiem1.DisplayMember = qLTH.tbl_HanhKiem.TenColumn.ColumnName;
            grc_HanhKiem1.ValueMember = qLTH.tbl_HanhKiem.idColumn.ColumnName;

            // 
            // grc_LenLop1
            // 
            this.grc_LenLop1.HeaderText = "Lên lớp";
            this.grc_LenLop1.Name = "grc_LenLop1";
            this.grc_LenLop1.Width = 200;
            grc_LenLop1.DataSource = qLTH.tbl_LenLop;
            grc_LenLop1.DisplayMember = qLTH.tbl_LenLop.TenColumn.ColumnName;
            grc_LenLop1.ValueMember = qLTH.tbl_LenLop.idColumn.ColumnName;
            // 
            // grc_LopKe
            // 
            this.grc_LopKe.HeaderText = "Chuyển lên";
            this.grc_LopKe.Name = "grc_LopKe";
            grc_LopKe.DataSource = dataLop;
            grc_LopKe.DisplayMember = "ten";
            grc_LopKe.ValueMember = "id";
            #endregion
            
            for (int i = 0; i < data.Columns.Count; i++)
            {
                dg_DanhSach.Columns[i + 1].DataPropertyName = data.Columns[i].ColumnName;
                dg_DanhSach.Columns[i + 1].ReadOnly = true;
                dg_DanhSach.Columns[i + 1].SortMode = DataGridViewColumnSortMode.Automatic;
            }
            grc_LenLop1.ReadOnly =
                grc_LopKe.ReadOnly = false;
            // -------------------------
            tblBinding.DataSource = data;
            // -------------------------

            fillData();
        }

        private void loadDataLop(int _idLop)
        {
            view_HocSinhTableAdapter hsAdap = new view_HocSinhTableAdapter();
            qh_LopMonHocTableAdapter lopMonHocAdap = new qh_LopMonHocTableAdapter();

            lopMonHocAdap.Connection.ConnectionString = 
                hsAdap.Connection.ConnectionString = PublicLib.getStrConn();

            hsAdap.ClearBeforeFill = true;
            hsAdap.FillByLopCu(qLTH.view_HocSinh, idNamHoc, _idLop);

            lopMonHocAdap.ClearBeforeFill = true;
            lopMonHocAdap.FillByLop(qLTH.qh_LopMonHoc, _idLop);
            //data.Rows.Clear();
                
            qh_HocSinhMonHocTableAdapter diemAdp = new qh_HocSinhMonHocTableAdapter();
            diemAdp.Connection.ConnectionString = PublicLib.getStrConn();
            diemAdp.ClearBeforeFill = true;

            tbl_HocBaTableAdapter hocbaAdap = new tbl_HocBaTableAdapter();
            hocbaAdap.Connection.ConnectionString = PublicLib.getStrConn();
            idNamHoc = (int)qLTH.tbl_NamHoc[tscb_NamHoc.SelectedIndex][qLTH.tbl_NamHoc.idColumn];
            hocbaAdap.FillByNamHoc(qLTH.tbl_HocBa, idNamHoc);

            for (int i = 0; i < qLTH.view_HocSinh.Rows.Count; i++)
            {
                DataRow nRow = data.NewRow();
                int idHS = (int)qLTH.view_HocSinh.Rows[i][qLTH.view_HocSinh.idColumn];
                #region Thong Tin Hoc Sinh
                nRow[CLN_ID] = idHS; // id
                nRow[CLN_ID_LOP] = _idLop; // id
                nRow[CLN_HO_DEM] = qLTH.view_HocSinh.Rows[i][qLTH.view_HocSinh.HoDemColumn]; // Ho Dem
                nRow[CLN_TEN] = qLTH.view_HocSinh.Rows[i][qLTH.view_HocSinh.TenColumn]; // Ten
                int gt = int.Parse(qLTH.view_HocSinh.Rows[i][qLTH.view_HocSinh.GioiTinhColumn].ToString()); // GioiTinh
                nRow[CLN_GIOI_TINH] = (gt == 0 ? "Nam" : "Nữ");
                nRow[CLN_NGAY_SINH] = qLTH.view_HocSinh.Rows[i][qLTH.view_HocSinh.NgaySinhColumn]; // Ngay Sinh
                #endregion
                #region Tinh Hoc Luc
                MyDataSet.QLTH.qh_HocSinhMonHocDataTable diem_HK1 = new QLTH.qh_HocSinhMonHocDataTable(),
                                                         diem_HK2 = new QLTH.qh_HocSinhMonHocDataTable();

                diemAdp.FillByHocSinhHocKy(diem_HK1, idHS, (int)qLTH.tbl_HocKy[0][qLTH.tbl_HocKy.idColumn]);
                diemAdp.FillByHocSinhHocKy(diem_HK2, idHS, (int)qLTH.tbl_HocKy[1][qLTH.tbl_HocKy.idColumn]);

                float sum = 0, result = 0;
                int dv = 0, dvKy1 = 0, dvKy2 = 0;
                float diemMin = 100000, 
                      tbKy1 = 0, tbKy2 = 0, 
                      diemToan = 0, DiemVan = 0, diemMonChuyen = -1; // fix id mon toan la 1, mon van la 2

                for (int j = 0; j < qLTH.qh_LopMonHoc.Rows.Count; j++)
                {
                    float diemCaNam = 0;
                    int heso;
                    if (qLTH.qh_LopMonHoc[j][qLTH.qh_LopMonHoc.HeSoColumn] != DBNull.Value)
                        heso = (int)qLTH.qh_LopMonHoc[j][qLTH.qh_LopMonHoc.HeSoColumn];
                    else
                        heso = 1; // default he so mon hoc se la 1

                    DataRow[] tmpRow1 = diem_HK1.Select(qLTH.qh_HocSinhMonHoc.idMonHocColumn.ColumnName + "=" + qLTH.qh_LopMonHoc[j][qLTH.qh_LopMonHoc.idMonColumn].ToString());
                    DataRow[] tmpRow2 = diem_HK2.Select(qLTH.qh_HocSinhMonHoc.idMonHocColumn.ColumnName + "=" + qLTH.qh_LopMonHoc[j][qLTH.qh_LopMonHoc.idMonColumn].ToString());
                    if (tmpRow1.Length > 0 && tmpRow2.Length > 0)
                    {   // Mon duoc hoc trong ca 2 hoc ky
                        float hk1 = 0;
                        float hk2 = 0;
                        hk1 = (float)tmpRow1[0][qLTH.qh_HocSinhMonHoc.DiemTBColumn.ColumnName];
                        hk2 = (float)tmpRow2[0][qLTH.qh_HocSinhMonHoc.DiemTBColumn.ColumnName];
                        diemCaNam = (hk2 * 2 + hk1) / 3;
                        dv += heso;

                        tbKy1 += heso * hk1;
                        tbKy2 += heso * hk2;
                        dvKy1 += heso;
                        dvKy2 += heso;
                    }
                    else
                    { 
                        // chi 1 ky hoc
                        if (tmpRow1.Length > 0)
                        {
                            float hk1 = (float)tmpRow1[0][qLTH.qh_HocSinhMonHoc.DiemTBColumn.ColumnName];
                            diemCaNam = hk1;
                            dv += heso;

                            tbKy1 += heso * hk1;
                            dvKy1 += heso;
                        }

                        if (tmpRow2.Length > 0)
                        {
                            float hk2 = (float)tmpRow2[0][qLTH.qh_HocSinhMonHoc.DiemTBColumn.ColumnName];
                            diemCaNam = hk2;
                            dv += heso;

                            tbKy2 += heso * hk2;
                            dvKy2 += heso;
                        }

                    }

                    diemCaNam = float.Parse(diemCaNam.ToString(PConst.LAM_TRON)); // lam tron
                    sum += heso * diemCaNam;

                    if (diemCaNam < diemMin) diemMin = diemCaNam;
                    if ((int)(qLTH.qh_LopMonHoc[j][qLTH.qh_LopMonHoc.idMonColumn]) == 1) diemToan = diemCaNam;
                    if ((int)(qLTH.qh_LopMonHoc[j][qLTH.qh_LopMonHoc.idMonColumn]) == 2) DiemVan = diemCaNam;
                    if (qLTH.qh_LopMonHoc[j][qLTH.qh_LopMonHoc.MonChuyenColumn] != DBNull.Value)
                        if ((Boolean)qLTH.qh_LopMonHoc[j][qLTH.qh_LopMonHoc.MonChuyenColumn])
                            diemMonChuyen = diemCaNam;
                }

                if (dv != 0) result = sum / dv;

                if (dvKy1 != 0) // tb hoc ky 1
                    nRow[CLN_HK_I] = (tbKy1 / dvKy1).ToString(PConst.LAM_TRON); // hoc ky 1
                else
                    nRow[CLN_HK_I] = 0;
                if (dvKy2 != 0) // tb hoc ky 1
                    nRow[CLN_HK_II] = (tbKy2 / dvKy2).ToString(PConst.LAM_TRON); // hoc ky 2
                else
                    nRow[CLN_HK_II] = 0;

                nRow[CLN_CA_NAM] = (((float)nRow[CLN_HK_I] + 2 * (float)nRow[CLN_HK_II])/3).ToString(PConst.LAM_TRON); // diem tb ca nam

                if (diemMonChuyen == -1)
                    nRow[CLN_HOC_LUC] = PublicLib.XetHocLuc_LopThuong(diemToan, DiemVan, diemMin, result);
                else
                    nRow[CLN_HOC_LUC] = PublicLib.XetHocLuc_LopChuyen(diemMonChuyen, diemMin, result);
                #endregion
                #region Hanh Kiem

                DataRow[] hkRow = qLTH.tbl_HocBa.Select(qLTH.tbl_HocBa.idHocSinhColumn.ColumnName + "=" + idHS);
                if (hkRow.Length > 0)
                {
                    int hkk1 = PConst.HK_YEU;
                    if (hkRow[0][qLTH.tbl_HocBa.idHanhKiemHK1Column] != DBNull.Value)
                        hkk1 = (int)hkRow[0][qLTH.tbl_HocBa.idHanhKiemHK1Column];

                    int hkk2 = PConst.HK_YEU;
                    if (hkRow[0][qLTH.tbl_HocBa.idHanhKiemHK2Column] != DBNull.Value)
                        hkk2 = (int)hkRow[0][qLTH.tbl_HocBa.idHanhKiemHK2Column];

                    int hkcn = (hkk1 + 2 * hkk2) / 3;

                    nRow[CLN_HANH_KIEM] = hkcn;
                }
                #endregion
                #region Xet Len Lop
                int soNgayNghi = 0;
                DataRow[] hbRow = qLTH.tbl_HocBa.Select(qLTH.tbl_HocBa.idHocSinhColumn.ColumnName + "=" + idHS);
                if (hbRow.Length > 0)
                {
                    if (hbRow[0][qLTH.tbl_HocBa.SoNgayNghiColumn] != DBNull.Value)
                        soNgayNghi = (int)hbRow[0][qLTH.tbl_HocBa.SoNgayNghiColumn];
                }

                if ((int)nRow[CLN_HOC_LUC] <= PConst.HL_TRUNG_BINH && // xep loai hoc luc tu trung binh tro len
                      (int)nRow[CLN_HANH_KIEM] <= PConst.HK_TB && // Xep loai HK tu trung binh tro len
                         soNgayNghi <= 45) // nghi it hon 45 ngay
                {
                    nRow[CLN_LEN_LOP] = PConst.LL_LEN_THANG; // duoc len lop
                    nRow[CLN_LOP_KE] = qLTH.tbl_LopHoc.Select(qLTH.tbl_LopHoc.idColumn.ColumnName + "=" + _idLop)[0][qLTH.tbl_LopHoc.idLopKeTiepColumn];
                }
                else
                {
                    nRow[CLN_LEN_LOP] = PConst.LL_KHONG_DU_DK; // Khong du dieu kien
                    nRow[CLN_LOP_KE] = _idLop;
                }

                // Xem trong CSDL da co chua
                if (hbRow.Length > 0)
                {
                    if (hbRow[0][qLTH.tbl_HocBa.idLenLopColumn] != DBNull.Value)
                    {
                        nRow[CLN_LEN_LOP] = hbRow[0][qLTH.tbl_HocBa.idLenLopColumn];
                    }
                }
                #endregion
                data.Rows.Add(nRow);
            }

        }

        private void fillData()
        {
            if (state == PConst.STATE_LOAD) return;
            if (tscb_Lop.SelectedIndex == -1) return;
            data.Rows.Clear();
            if (tscb_Lop.SelectedIndex < tscb_Lop.Items.Count - 1)
            {
                // Tung lop rieng
                loadDataLop((int)qLTH.tbl_LopHoc[tscb_Lop.SelectedIndex][qLTH.tbl_LopHoc.idColumn]);
                dg_DanhSach.Sort(dg_DanhSach.Columns[grc_Ten.Name], ListSortDirection.Ascending);
                dg_DanhSach.Columns[grc_Ten.Name].HeaderCell.SortGlyphDirection = SortOrder.Ascending;
            }
            else
            { // Tat ca
                for (int i = 0; i < tscb_Lop.Items.Count - 1; i++)
                {
                    loadDataLop((int)qLTH.tbl_LopHoc[i][qLTH.tbl_LopHoc.idColumn]);
                }
            }
        }

        private void refreshData()
        {
            reloadComboBox();
            reloadDataTable();
        }

        private void QuanLyGiaoVien_Resize(object sender, EventArgs e)
        {
            dg_DanhSach.Width = this.Width - 5;
            dg_DanhSach.Height = stt_Bottom.Top - dg_DanhSach.Top - 1;
        }

        private void dg_DanhSach_DataBindingComplete(object sender, EventArgs e)
        {
            for (int i = 0; i < dg_DanhSach.RowCount; i++)
            {
                dg_DanhSach.Rows[i].Cells[0].Value = i + 1;
                dg_DanhSach.Rows[i].DefaultCellStyle.BackColor = (i % 2 == 1) ? PConst.COLOR_BACK_1 : PConst.COLOR_BACK_DEF;
            }
        }

        private void tscb_Lop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (state == PConst.STATE_LOAD)
                return;

            fillData();
            //reloadDataTable();
        }

        private void tscb_NamHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tscb_NamHoc.SelectedIndex == -1)
                return;
            
            MyDataSet.QLTHTableAdapters.tbl_HocKyTableAdapter namAdap = new tbl_HocKyTableAdapter();
            namAdap.Connection.ConnectionString = PublicLib.getStrConn();
            idNamHoc = int.Parse(qLTH.tbl_NamHoc.Rows[tscb_NamHoc.SelectedIndex][qLTH.tbl_NamHoc.idColumn].ToString());
            namAdap.FillByNamHoc(qLTH.tbl_HocKy, idNamHoc);
            if (state != PConst.STATE_LOAD) 
            {
                fillData();
            }
                //reloadDataTable();
        }

        private void saveData()
        {
            //save ket qua xet len lop cho tung hoc sinh
            tbl_HocBaTableAdapter hocbaAdap = new tbl_HocBaTableAdapter();
            hocbaAdap.Connection.ConnectionString = PublicLib.getStrConn();
            idNamHoc = (int)qLTH.tbl_NamHoc[tscb_NamHoc.SelectedIndex][qLTH.tbl_NamHoc.idColumn];

            hocbaAdap.FillByNamHoc(qLTH.tbl_HocBa, idNamHoc);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                int idHS = (int)data.Rows[i][0];
                DataRow[] hkRow = qLTH.tbl_HocBa.Select(qLTH.tbl_HocBa.idHocSinhColumn.ColumnName + "=" + idHS);
                object updateValue = DBNull.Value;
                if (data.Rows[i][data.Columns.Count - 2] != null) // len lop
                    updateValue = data.Rows[i][data.Columns.Count - 2];

                if (hkRow.Length > 0)
                {
                    hkRow[0][qLTH.tbl_HocBa.idLenLopColumn] = updateValue;
                }
                else
                {
                    DataRow nRow = qLTH.tbl_HocBa.NewRow();
                    nRow[qLTH.tbl_HocBa.idHocSinhColumn] = idHS;
                    nRow[qLTH.tbl_HocBa.idNamHocColumn] = idNamHoc;
                    nRow[qLTH.tbl_HocBa.idLenLopColumn] = updateValue;
                    qLTH.tbl_HocBa.Rows.Add(nRow);
                }
            }

            hocbaAdap.Update(qLTH.tbl_HocBa);
            // -----------
            // Chuyen Len lop cho nam hoc moi
            if (tscb_NamHoc.SelectedIndex == tscb_NamHoc.Items.Count - 1)
            {
                // Chi lam cho nam hoc hien tai
                if (MessageBox.Show(MSG.MSG_TAO_NAM_HOC_MOI, "", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
                {
                    MyControls.PopupTextBox frm_NamHocMoi = new QuanLyTruongHoc_App.MyControls.PopupTextBox();
                    frm_NamHocMoi.InputText = QLNamHoc.createHocKyName();
                    frm_NamHocMoi.MdiParent = this.MdiParent;
                    frm_NamHocMoi.Show();
                    frm_NamHocMoi.OkClicked += new QuanLyTruongHoc_App.MyControls.PopupTextBox.EventUpdate(frm_NamHocMoi_OkClicked);
                }
            }
        }

        private void frm_NamHocMoi_OkClicked(object sender)
        {
            String txt = ((MyControls.PopupTextBox)sender).InputText;
            tbl_NamHocTableAdapter namAdap = new tbl_NamHocTableAdapter();
            namAdap.Connection.ConnectionString = PublicLib.getStrConn();

            int newID = PublicLib.getMaxIdFromTbl(qLTH.tbl_NamHoc.TableName, qLTH.tbl_NamHoc.idColumn.ColumnName) + 1;
            DataRow nRow = qLTH.tbl_NamHoc.NewRow();
            nRow[qLTH.tbl_NamHoc.idColumn] = newID;
            nRow[qLTH.tbl_NamHoc.TenColumn] = txt;
            nRow[qLTH.tbl_NamHoc.flagColumn] = PConst.FLAG_NONE;
            qLTH.tbl_NamHoc.Rows.Add(nRow);

            namAdap.Update(nRow);
            // Them Hoc Ky
            tbl_HocKyTableAdapter hocKyAdap = new tbl_HocKyTableAdapter();
            hocKyAdap.Connection.ConnectionString = PublicLib.getStrConn();
            for (int i = 0; i < 2; i++)
            {
                int nId = PublicLib.getMaxIdFromTbl(qLTH.tbl_HocKy.TableName, qLTH.tbl_HocKy.idColumn.ColumnName) + 1;
                DataRow hkRow = qLTH.tbl_HocKy.NewRow();
                hkRow[qLTH.tbl_HocKy.idColumn] = nId;
                hkRow[qLTH.tbl_HocKy.idNamHocColumn] = newID;
                hkRow[qLTH.tbl_HocKy.tenColumn] = "Học Kỳ " + (i == 0 ? "I" : "II");
                qLTH.tbl_HocKy.Rows.Add(hkRow);

                hocKyAdap.Update(hkRow);
            }
             // --------------------------------------
            qh_DSThiTotNghiepTableAdapter dsTN = new qh_DSThiTotNghiepTableAdapter();
            qh_LopHocSinhTableAdapter lopHSAdap = new qh_LopHocSinhTableAdapter();
            tbl_HocSinhTableAdapter hsAdap = new tbl_HocSinhTableAdapter();
            lopHSAdap.Connection.ConnectionString = 
                hsAdap.Connection.ConnectionString = dsTN.Connection.ConnectionString =  PublicLib.getStrConn();
            hsAdap.FillByFlag(qLTH.tbl_HocSinh, PConst.FLAG_NONE);
            dsTN.FillByNamHoc(qLTH.qh_DSThiTotNghiep, idNamHoc);

            for (int i = 0; i < data.Rows.Count; i++)
            {
                DataRow[] hsRow = qLTH.tbl_HocSinh.Select(qLTH.tbl_HocSinh.idColumn.ColumnName + "=" + data.Rows[i][0].ToString());

                int idLopKe = -1;
                if (data.Rows[i][data.Columns.Count - 1] != DBNull.Value)
                    idLopKe = (int)data.Rows[i][data.Columns.Count - 1];

                if (idLopKe != -1)
                {
                    // HS len lop tiep
                    DataRow tmpRow = qLTH.qh_LopHocSinh.NewRow();
                    tmpRow[qLTH.qh_LopHocSinh.idHocSinhColumn] = data.Rows[i][0]; // id Hoc Sinh
                    tmpRow[qLTH.qh_LopHocSinh.idLopColumn] = idLopKe; // id lop moi
                    tmpRow[qLTH.qh_LopHocSinh.idNamHocColumn] = newID; // id Hoc Sinh

                    qLTH.qh_LopHocSinh.Rows.Add(tmpRow);

                    if (hsRow.Length > 0)
                        hsRow[0][qLTH.tbl_HocSinh.idLopColumn] = idLopKe;
                }
                else
                {
                    // HS Tot nghiep
                    if (hsRow.Length > 0)
                        hsRow[0][qLTH.tbl_HocSinh.FlagColumn] = PConst.FLAG_CHO_THI_TN;

                    if (qLTH.qh_DSThiTotNghiep.Select(qLTH.qh_DSThiTotNghiep.idHocSinhColumn.ColumnName +
                                                        "=" + data.Rows[i][0].ToString()).Length == 0) // chi them trong TH chua co
                    {
                        DataRow tmp = qLTH.qh_DSThiTotNghiep.NewRow();
                        tmp[qLTH.qh_DSThiTotNghiep.idHocSinhColumn.ColumnName] = data.Rows[i][0];
                        tmp[qLTH.qh_DSThiTotNghiep.idNamHocColumn.ColumnName] = idNamHoc;
                        tmp[qLTH.qh_DSThiTotNghiep.KetQuaColumn.ColumnName] = DBNull.Value; // chua dat

                        qLTH.qh_DSThiTotNghiep.Rows.Add(tmp);
                    }
                }
            }

            try
            {
                hsAdap.Update(qLTH.tbl_HocSinh);
                lopHSAdap.Update(qLTH.qh_LopHocSinh);
                dsTN.Update(qLTH.qh_DSThiTotNghiep);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Loi :" + ex.Message);
            }
        }

        private void tsb_Luu_Click(object sender, EventArgs e)
        {
            dg_DanhSach.EndEdit();
            saveData();
        }

        private void XetLenLop_Activated(object sender, EventArgs e)
        {
            checkUserPower();
        }

        private void tsb_In_Click(object sender, EventArgs e)
        {
            PrintDataGrid.PrintDGV print = new PrintDataGrid.PrintDGV();
            print.Print_DataGridView(dg_DanhSach);
        }
    }
}
